package com.kye.express.business.found;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.jaeger.library.StatusBarUtil;
import com.kye.base.Logger;
import com.kye.express.R;
import com.kye.express.business.base.BaseAppCompatFragment;
import com.kye.express.business.found.adapter.FoundViewPageAdapter;
import com.kye.express.databinding.FragmentFoundBinding;
import com.kye.express.utils.AppUtil;

/**
 * 发现
 */

public class FoundFragment extends BaseAppCompatFragment implements FoundContract.FoundView {

    private static final String TAG = FoundFragment.class.getSimpleName();
    protected FragmentFoundBinding mainFoundBinding;
    private String[] titles = {
            "福利",
            "新闻",
            "视频"
    };
    protected FoundViewPageAdapter pageAdapter;
    protected FoundContract.FoundPresenter presenter;

    public static FoundFragment newInstance() {
        Bundle args = new Bundle();
        FoundFragment fragment = new FoundFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_found, container, false);
        StatusBarUtil.setColor(getActivity(), getResources().getColor(R.color.purple_0), 0);
        mainFoundBinding = DataBindingUtil.bind(view);
        initViews();
        return view;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initEvents();
        presenter = new FoundContract.FoundPresenter();
        presenter.bind(this);
    }

    private void initViews() {
        pageAdapter = new FoundViewPageAdapter(getChildFragmentManager());
        mainFoundBinding.vpPages.setAdapter(pageAdapter);
        mainFoundBinding.vpPages.setOffscreenPageLimit(titles.length);
        mainFoundBinding.tabLayout.setViewPager(mainFoundBinding.vpPages, titles);
        mainFoundBinding.vpPages.setCurrentItem(0);

        adaptationHeight();
    }

    private void initEvents() {

    }

    @Override
    protected void initData(@Nullable Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        presenter.unbind();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public void adaptationHeight(){
        if(AppUtil.getSDKVersion() == 19 || AppUtil.getSDKVersion() == 20){
            FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0, AppUtil.getStatusBarHeight(), 0, 0);
            mainFoundBinding.linearlayoutFound.setLayoutParams(lp);
        }
    }
}
